import time

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from tests.litemall_sys.pages.basepage import BasePage
# from tests.litemall_sys.pages.goods_list import GoodsList


# from selenium.webdriver.common.by import By

class AddGoods(BasePage):
    # 【商品列表】页面进入：
    __GOODS_ADD = (By.CSS_SELECTOR, '.app-container>div>button:nth-of-type(2)')
    # 【商品介绍】
    __GOODS_NUM = (By.CSS_SELECTOR, '.el-form>div:nth-child(1)>div>div>input')   # 商品编号
    __GOODS_NUM_ERROR = (By.CSS_SELECTOR, '.el-form>div:nth-child(1)>div>div:nth-child(2)') # “商品编号不能为空”
    __GOODS_NAME = (By.CSS_SELECTOR, '.el-form>div:nth-child(2)>div>div>input') # 商品名称
    __GOODS_NAME_ERROR = (By.CSS_SELECTOR, '.el-form>div:nth-child(2)>div>div:nth-child(2)') # “商品名称不能为空”
    __GOODS_price = (By.CSS_SELECTOR, '.el-form>div:nth-child(3)>div>div>input') # （商品）市场售价
    # __IS_NEW_GOODS = (By.XPATH, '//*[text()="新品"]//..//input') # 【是否新品】的按钮-新品
    # __IS_OLD_GOODS = (By.XPATH, '//*[text()="非新品"]//..//input') # 【是否新品】的按钮-非新品
    __is_new_goods = (By.XPATH, '//*[text()="新品"]//..//span[@class="el-radio__inner"]')
    _is_old_locate = (By.XPATH,'//*[text()="非新品"]//..//span[@class="el-radio__inner"]')
    __ordinary_GOODS = (By.XPATH, '//*[text()="普通"]//..//input') # 【是否热卖】的按钮-普通
    __HOT_SALE_GOODS = (By.XPATH, '//*[text()="热卖"]//..//input') # 【是否热卖】的按钮-热卖
    __ON_SALE_GOODS = (By.XPATH, '//*[text()="在售"]//..//input') # 【是否在售】的按钮-在售
    __Not_SALE_GOODS = (By.XPATH, '//*[text()="未售"]//..//input') # 【是否新品】的按钮-未售
    __GOODS_IMG_ICON = (By.CSS_SELECTOR, '.el-form>div:nth-child(7)>div>div>div>input') # 商品图片上传
    __GOODS_public_ICON = (By.CSS_SELECTOR, '.el-form>div:nth-child(8)>div>div>div>input') # 宣传画廊上传

    __GOODS_UNIT = (By.XPATH, '//*[@placeholder="件 / 个 / 盒"]') # 商品单位
    __GOODS_keywords = (By.XPATH, '//*[text()="+ 增加"]//..')   # 关键字
    """
    所属分类和品牌商怎么写？要不要判断的。
    """
    __GOODS_category = (By.CSS_SELECTOR, '.el-form>div:nth-child(11)>div>div>div>input')   # 所属分类
    __GOODS_brand = (By.CSS_SELECTOR, '.el-form>div:nth-child(12)>div>div>div>input') # 品牌商
    __GOODS_STORY = (By.CSS_SELECTOR, '.el-form>div:nth-child(13)>div>div>input')   # 商品简介
    """
    商品详细介绍:iframe--这里有这个标签，要写进入进出吗？还是在test里面写。
    """
    __GOODS_INTRO = (By.XPATH, '//*[@data-mce-bogus="1"]')  # 商品介绍详情<p>标签内输入内容。
    """
    【商品规格】
    """
    __GOODS_Default_start =(By.XPATH, '//*[text()="默认标准规格"]//..//input')   # 默认规格标准
    __GOODS_Multi_start =(By.XPATH, '//*[text()="多规格支持"]//..//input')   # 多规格支持
    __GOODS_Multi_start_ADD = (By.CSS_SELECTOR,'.el-col.el-col-10>button')  # 多规格支持添加按钮。
    # 多规格添加页面内容：
    __MultiD_NAME = (By.CSS_SELECTOR, '[aria-label="设置规格"]>div:nth-child(2)>form>div>div>div>input')    # 规格名
    __MultiD_value = (By.CSS_SELECTOR, '[aria-label="设置规格"]>div:nth-child(2)>form>div:nth-child(2)>div>div>input')  # 规格值
    __MultiD_img = (By.CSS_SELECTOR, '[aria-label="设置规格"]>div:nth-child(2)>form>div:nth-child(3)>div>div>div>input')    # 规格图片
    __MutilD_cancel = (By.CSS_SELECTOR, '[aria-label="设置规格"]>div:nth-child(3)>div>button:nth-child(1)')   # 取消规格添加
    __MutilD_confirm = (By.CSS_SELECTOR, '[aria-label="设置规格"]>div:nth-child(3)>div>button:nth-child(1)')   # 确认规格添加

    __GOODS_stock = (By.CSS_SELECTOR, '.el-table__body>tbody>tr>td:nth-child(5)>div>button')# 库存设置按钮：//*[text()="设置"]//..
    # 商品库存设置
    __stock_price = (By.CSS_SELECTOR ,'[aria-label="添加货品"]>div:nth-child(2)>form>div:nth-child(2)>div>div>input')   # 货品售价
    __stock_amount = (By.CSS_SELECTOR ,'[aria-label="添加货品"]>div:nth-child(2)>form>div:nth-child(3)>div>div>input')   # 货品数量
    __stock_img = (By.CSS_SELECTOR, '[aria-label="添加货品"]>div:nth-child(2)>form>div:nth-child(4)>div>div>div>input')     # 货品图片
    __stock_cancel = (By.CSS_SELECTOR, '[aria-label="添加货品"]>div:nth-child(3)>div>button:nth-child(1)')     # 取消添加
    __stock_confirm = (By.CSS_SELECTOR, '[aria-label="添加货品"]>div:nth-child(3)>div>button:nth-child(2)')     # 确认添加

    __GOODS_parameter = (By.CSS_SELECTOR, '.app-container>div:nth-child(4)>div>button') # 商品参数添加
    # 商品参数设置
    __parameter_name = (By.CSS_SELECTOR, '[aria-label="添加商品参数"]>div:nth-child(2)>form>div>div>div>input')   # 参数名称
    __parameter_value = (By.CSS_SELECTOR, '[aria-label="添加商品参数"]>div:nth-child(2)>form>div:nth-child(2)>div>div>input') # 参数值
    __parameter_cancel = (By.CSS_SELECTOR, '[aria-label="添加商品参数"]>div:nth-child(3)>div>button:nth-child(1)')    # 取消添加
    __parameter_confirm = (By.CSS_SELECTOR, '[aria-label="添加商品参数"]>div:nth-child(3)>div>button:nth-child(2)')    # 确认添加
    # 【整个添加页面是否上架】
    __GOODS_ADD_list=(By.CSS_SELECTOR, '.op-container>button:nth-child(2)') # 商品上架按钮
    __GOODS_cancel = (By.CSS_SELECTOR, '.op-container>button:nth-child(1)')  # 商品上架页面 【取消】添加按钮。

    __goods_repeat = (By.XPATH, '//*[text()="业务错误：商品名已经存在"]') # 商品名称重复的弹窗
    __click_sure = (By.CSS_SELECTOR, '.el-message-box__btns>button:nth-child(1)') # 商品名称重复弹窗的确定按钮
    __ele_error = (By.XPATH, '//*[text()="业务错误：参数不对"]') # 必填项未填写弹窗
    __error_sure = (By.CSS_SELECTOR, '.el-message-box__btns>button:nth-child(1)') # 必填项未填写弹窗的确定按钮
    ele = (By.XPATH, '//*[text()="当前价格"]')

    """
    【必填验证】
    """
    def add_goods(self, info):
        """
        添加商品信息:仅商品编号和商品名称添加(必填的)
        """
        from tests.litemall_sys.pages.goods_list import GoodsList
        goods_info = info[0]    # 解包字典
        goods_num = goods_info['goods_num']
        goods_name = goods_info['goods_name']
        with allure.step("点击【添加】按钮进入商品上架页面"):
            self.find_click(*self.__GOODS_ADD)  # 进入添加页面
        # 【商品介绍】
        with allure.step("商品上架页面，添加【商品介绍】并保存"):
            self.find_and_send(*self.__GOODS_NUM, text=goods_num)     # 输入商品编号
            self.find_and_send(*self.__GOODS_NAME, text=goods_name)    # 输入商品名称
        with allure.step("确认上架"):
            self.find_click(*self.__GOODS_ADD_list)
            self.wait(self.until_wait(button_element=self.__GOODS_ADD_list, until_ele=self.__GOODS_ADD))
        return GoodsList(self.driver)

    def add_goods_enter(self):
        with allure.step("点击【添加】按钮进入商品上架页面"):
            self.find_click(*self.__GOODS_ADD)  # 进入添加页面
        return self

    def add_goods_num(self, info):
        # from tests.litemall_sys.pages.goods_list import GoodsList
        with allure.step(f"商品上架页面，输入【商品编号】{info}"):
            self.find_and_send(*self.__GOODS_NUM, text=info)     # 输入商品编号
        return self

    def add_goods_name(self, info):
        with allure.step("商品上架页面，添加【商品名称】并保存"):
            self.find_and_send(*self.__GOODS_NAME, text=info)    # 输入商品名称
        return self

    def add_goods_price(self, info):
        with allure.step(f"商品上架页面，输入【商品价格】{info}"):
            self.find_and_send(*self.__GOODS_price, text=info)  # 输入商品价格
        return self
    def add_goods_is_new(self):
        with allure.step("点击‘是新品’按钮"):
            self.find_click(*self.__is_new_goods)  # 点击新品
        return self
    def add_goods_is_old(self):
        with allure.step("点击‘非新品’按钮"):
            self.find_click(*self._is_old_locate)  # 点击非新品
        return self
    def add_goods_ordinary(self):
        with allure.step("点击‘普通’按钮"):
            self.find_click(*self.__ordinary_GOODS)  # 点击【普通】商品
        return self
    def add_goods_hot_sale(self):
        with allure.step("点击‘热卖’按钮"):
            self.find_click(*self.__HOT_SALE_GOODS)  # 点击【热卖】商品
        return self
    def add_goods_on_sale(self):
        with allure.step("点击‘在售’按钮"):
            self.find_click(*self.__ON_SALE_GOODS)  # 点击【在售】
        return self
    def add_goods_not_sale(self):
        with allure.step("点击‘未售’按钮"):
            self.find_click(*self.__Not_SALE_GOODS)  # 点击【未售】
        return self
    def __add_goods(self, imgs):
        with allure.step("【商品图片】和【宣传画廊】上传图片"):  # ###这边可以使用send吗?
            self.find_and_send(*self.__GOODS_IMG_ICON, text=imgs)  # 商品图片

    """保存【确认上架】"""
    def add_goods_save(self):
        from tests.litemall_sys.pages.goods_list import GoodsList
        with allure.step("确认上架"):
            # self.find_click(*self.__GOODS_ADD_list)
            self.wait(self.until_wait(button_element=self.__GOODS_ADD_list, until_ele=self.ele))
        return GoodsList(self.driver)

    """【取消按钮】"""
    def add_goods_cancel(self):
        from tests.litemall_sys.pages.goods_list import GoodsList
        with allure.step("取消上架"):
            self.find_click(*self.__GOODS_cancel)
        self.wait(self.until_wait(button_element=self.__GOODS_ADD_list, until_ele=self.__GOODS_ADD))
        return GoodsList(self.driver)

    def add_goods_repeat(self):
        with allure.step("确认上架"):
            self.find_click(*self.__GOODS_ADD_list)
        self.wait(self.until_wait(self.__GOODS_ADD_list, self.__goods_repeat))
        with allure.step("弹出商品名称重复的窗口"):
            ele = self.find(*self.__goods_repeat).text
            self.find_click(*self.__click_sure)
            self.wait(self.until_wait(self.__click_sure, self.__GOODS_ADD_list))
            self.add_goods_cancel()
        return ele

    def not_add_name(self):
        with allure.step("确认上架"):
            self.find_click(*self.__GOODS_ADD_list)
        self.wait(self.until_wait(self.__GOODS_ADD_list, self.__ele_error))
        with allure.step("弹出参数错误窗口"):
            ele = self.find(*self.__ele_error).text
            self.driver.implicitly_wait(5)
            self.find_click(*self.__error_sure)
            self.wait(self.until_wait(self.__error_sure, self.__GOODS_ADD_list))
            self.add_goods_cancel()
        return ele





